//
//  JUFNComposeBtnView.swift
//  WiBoPM
//
//  Created by 陈俊峰 on 2017/1/13.
//  Copyright © 2017年 陈俊峰. All rights reserved.
/*
 搭建UI:
    一个slogan的图片
    和一群button
 */

import UIKit
import pop

class JUFNComposeBtnView: UIView {

    var tragetVC: UIViewController?
    var composeBtns: [JUFNComposeButton]?
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        setupUI()
        self.backgroundColor = UIColor.red
      
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
         addSubview(shotImage)
        addSubview(sloganImage)
       
        setupComposeBtn()
        sloganImage.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
    }
    //提供一个方法,把自己添加到当前控制器的view上去
    func showShotView(targetVC:UIViewController) {
        targetVC.view.addSubview(self)
        self.tragetVC = targetVC
    }
    
  
    lazy var sloganImage: UIImageView = UIImageView(image: #imageLiteral(resourceName: "compose_slogan"))
    lazy var shotImage: UIImageView = UIImageView(image: UIImage.SnapShotCurrentScreen().applyExtraLightEffect())
    private func setupComposeBtn(){
        let composeBtnImages = [
            #imageLiteral(resourceName: "tabbar_compose_lbs"),
            #imageLiteral(resourceName: "tabbar_compose_idea"),
            #imageLiteral(resourceName: "tabbar_compose_more"),
            #imageLiteral(resourceName: "tabbar_compose_weibo"),
            #imageLiteral(resourceName: "tabbar_compose_music"),
            #imageLiteral(resourceName: "tabbar_compose_photo")
        ]
        let btnMargin: CGFloat = (screenWidth - 3*composeBtnW) / 4
        var tempBtns: [JUFNComposeButton] = [JUFNComposeButton]()
        for i in 0 ..< composeBtnImages.count{
            let row: Int = i/3
            let cols: Int = i%3
            let btnX = CGFloat(cols)*(btnMargin+composeBtnW) + btnMargin
            let btnY = CGFloat(row)*(btnMargin+composeBtnH) + screenHeight
            let currentComposeBtn = JUFNComposeButton()
            currentComposeBtn.setImage(composeBtnImages[i], for: .normal)
            currentComposeBtn.setTitle("nuan", for: .normal)
            currentComposeBtn.frame = CGRect(x: btnX, y: btnY, width: composeBtnW, height: composeBtnH)
            currentComposeBtn.titleLabel?.textAlignment = .center
            currentComposeBtn.setTitleColor(UIColor.darkGray, for: .normal)
            currentComposeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            self.addSubview(currentComposeBtn)
            //给按钮添加动画
            tempBtns.append(currentComposeBtn)
            currentComposeBtn.addTarget(self, action: #selector(composeBtnDidClicked), for: .touchUpInside)
            currentComposeBtn.pop_add(showAnimation(index: i, isPop: true, btnCenter: currentComposeBtn.center), forKey: nil)
        }
        composeBtns = tempBtns
    }
    @objc private func composeBtnDidClicked(clickedBtn:UIButton){
      UIView.animate(withDuration: 0.25, animations: {
        for tempBtn in self.composeBtns! {
            if tempBtn == clickedBtn {
                clickedBtn.transform = CGAffineTransform.init(scaleX: 2, y: 2)
            }else{
                tempBtn.transform = CGAffineTransform.init(scaleX: 0.5, y: 0.5)
            }
        }
      }, completion: { (_) in
        //moda一个新控制器
        let sendVC = JUFNSendWiBoViewController()
        let nav = UINavigationController(rootViewController: sendVC)
        self.tragetVC?.present(nav, animated: true, completion: nil)
        
      })
        
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for (index,dismissBtn) in (composeBtns?.reversed().enumerated())! {
            dismissBtn.pop_add(self.showAnimation(index: index, isPop: false, btnCenter: dismissBtn.center), forKey: nil)
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
            
            self.removeFromSuperview()
        }
    }
    
    private func showAnimation(index:Int,isPop:Bool,btnCenter:CGPoint) -> POPSpringAnimation?{
        let showAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        
        showAnimation?.springBounciness = 10
        
        let toValue = CGPoint(x: btnCenter.x, y: isPop ? btnCenter.y - 350 : btnCenter.y + 350)
        showAnimation?.toValue = toValue
        
        showAnimation?.beginTime = Double(index)*0.03 + CACurrentMediaTime()
        return showAnimation
    }
    
    
}
